package com.ruoyi.user;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.framework.web.service.SysPermissionService;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/h5/user")
public class UserControllerH5 extends BaseController {

    @Autowired
    private SysUserMapper sysUserMapper;
    @Autowired
    private SysPermissionService permissionService;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @GetMapping("/{username}")
    public AjaxResult getNickName(@PathVariable("username") String username)
    {
        SysUser user = sysUserMapper.selectUserByUserName(username);
        user.setAvatar(UrlUtils.getUrl() + user.getAvatar());
//        // 角色集合
//        Set<String> roles = permissionService.getRolePermission(user);
//        // 权限集合
//        Set<String> permissions = permissionService.getMenuPermission(user);
//        AjaxResult ajax = AjaxResult.success();
//        ajax.put("user", user);
//        ajax.put("roles", roles);
//        ajax.put("permissions", permissions);
        return AjaxResult.success(user);
    }

    /**
     * 通讯录
     * @return
     */
    @GetMapping("/list/{deptId}")
    public TableDataInfo list(@PathVariable("deptId") Long deptId){
        List<SysUser> list = sysUserRoleMapper.selectUserByDeptIdForWeber(deptId);
//        for (SysUser s:list)
//            System.out.println(s.getDeptName()+" "+s.getUserName());
        return getDataTable(list);

//        //筛查
//        Long[] longs = sysUserRoleMapper.selectUserIdByROleIdU();
//        SysUser sysUser = new SysUser();
//        sysUser.setLongs(longs);
//        List<SysUser> sysUsers = sysUserMapper.selectUserList(sysUser);
//        List<SysUser> sysUsers1 = new ArrayList<>();
//        System.out.println("sysUsers: "+sysUsers.size());
//        for(SysUser user:sysUsers){
//            System.out.println("user.getDeptId(): "+user.getDeptId()+"----"+deptId);
//            if(user.getDeptId() == deptId){
//                if(user.getAvatar() != null && !user.getAvatar().equals("")){
//                    user.setAvatar(UrlUtils.getUrl() + user.getAvatar());
//                }
//                sysUsers1.add(user);
//            }
//        }
//        return getDataTable(sysUsers1);
    }

}
